package test2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Arrays;

/**
 * @author: mornd
 * @dateTime: 2023/5/15 - 15:44
 */
public class App {
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("b1", B1.class, bg -> bg.setScope("prototype"));
        context.registerBean("b2", B2.class);
        context.registerBean(CommonAnnotationBeanPostProcessor.class);
        context.registerBean(AutowiredAnnotationBeanPostProcessor.class);
        context.refresh();

        B1 bean = context.getBean(B1.class);

        Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
        // spring 不记录多例bean的销毁，需要手动销毁
        context.getDefaultListableBeanFactory().destroyBean(bean);
        context.close();
    }

    static class B1  {
        private B2 b2;

        @Autowired
        public void setB121(B2 b2) {
            System.out.println(b2);
            this.b2 = b2;
        }

        @PostConstruct
        public void init() {
            System.out.println("init");
        }

        @PreDestroy
        public void destroy() {
            System.out.println("destroy");
        }
    }

    static class B2 {}
}
